import { Component, Watch } from 'vue-property-decorator';
import AbpBase from "@/lib/abpbase";
import PageRequest from "@/store/entities/page-request";

@Component
export default class ListMixin extends AbpBase {
  _pageLoading = false
  get pageLoading() {
    return this._pageLoading
  }
  set pageLoading(v) {
    this._pageLoading = v
    if (v) {
      (this.$Spin as any).show()
    } else {
      (this.$Spin as any).hide()
    }
  }
  
  createModalShow: boolean = false;
  importModalShow: boolean = false;

  actionCreateOrEdit() {
    // TODO: this.$store.commit("schoolDepart/createOrEdit", data);
  }
  createOrEdit() {
    this.actionCreateOrEdit();
    this.createModalShow = true;
  }

  // TODO: extends PageRequest
  pagerequest: PageRequest = new PageRequest();
  get pageSize() {
    return this.state && this.state.pageSize
  }
  get totalCount() {
    return this.state && this.state.totalCount
  }
  get currentPage() {
    return this.state && this.state.currentPage
  }
  get list() {
    return this.state && this.state.list
  }

  setCurrentPage(page) {
    // TODO: this.$store.commit("school/setCurrentPage", page);
  }
  pageChange(page: number) {
    this.setCurrentPage(page)
    this.getpage()
  }
  setPageSize(pageSize) {
    // TODO: this.$store.commit("school/setPageSize", pagesize);
  }
  pagesizeChange(pagesize: number) {
    this.setPageSize(pagesize)
    this.getpage()
  }
  async getAll() {
    // TODO: await this.$store.dispatch({ type: "school/getAll", data: this.pagerequest })
  }
  async getpage(flagResetPage ?: boolean) {
    if (flagResetPage) {
      this.setCurrentPage(1)
    }
    this.pagerequest.maxResultCount = this.pageSize;
    this.pagerequest.skipCount = (this.currentPage - 1) * this.pageSize;
    await this.getAll()
  }

  indexMethod(row) {
    return row._index + 1 + this.pageSize * (this.currentPage - 1)
  }
  columnIndex = {
    type: 'index',
    width: 70,
    align: 'center',
    title: '序号',
    indexMethod: (row) => {
      return this.indexMethod(row)
    },
  };
  actionEdit(row) {
    // TODO: this.$store.commit("schoolDepart/edit", row);
  }
  async actionDelete(row): Promise<any> {
    // TODO:
    // return new Promise((res, rej) => {
    //   this.$Modal.confirm({
    //     title: this.L("Tips"),
    //     content: this.L(`确认删除部门 (${params.row.departName})？`),
    //     okText: this.L("Yes"),
    //     cancelText: this.L("No"),
    //     onOk: async () => {
    //       await this.$store.dispatch({
    //         type: "schoolDepart/delete",
    //         data: params.row
    //       }).then(() => {
    //         res(1);
    //       }).catch(() => {
    //         rej();
    //       });
    //     },
    //     onCancel: () => {
    //       rej();
    //     },
    //   });
    // })
  }
  gotoLastPageAfterDeleting(deletedCount = 1) {
    if (this.list.length === deletedCount) {
      if (this.currentPage > 1) {
        this.setCurrentPage(this.currentPage - 1)
      }
    }
  }
  columnAction = {
    title: this.L("Actions"),
    key: "Actions",
    width: 150,
    render: (h: any, params: any) => {
      return h("div", [
        h(
          "Button",
          {
            props: {
              type: "primary",
              size: "small"
            },
            style: {
              marginRight: "5px"
            },
            on: {
              click: () => {
                this.actionEdit(params.row)
                this.createOrEdit()
              }
            }
          },
          this.L("Edit")
        ),
        h(
          "Button",
          {
            props: {
              type: "error",
              size: "small"
            },
            on: {
              click: async () => {
                await this.actionDelete(params.row)
                  .then((res) => {
                    if (res > 0) {
                      this.gotoLastPageAfterDeleting(res)
                      this.$Message.success('删除成功')
                      this.getpage()
                    }
                  })
                  .catch((err) => {
                    this.$Message.error('删除失败')
                  })
              }
            }
          },
          this.L("Delete")
        )
      ]);
    }
  };
  
  // TODO:
  columns: Array<any>;
  initColumns() {

  }
  // TODO
  async createdInit() {
    
  }
  async created() {
    this.initColumns()
    await this.createdInit()
  }

  // TODO
  async exportList() {
  }
}